Java split() method

    split() method

    This method will allow you to split the given string according to the specified expression, and the result is returned in the array of the char.

    Syntax-

    public String split(String regex)  

    and,

    public String split(String regex, int limit)  

    This method will throw PatternSyntaxException if you are providing the invalid regular expression to split the string.

    Example-

    public class Simple{  
       public static void main(String[] args) {      
    
          String s1="welcome to Java and welcome to tutorial";  
          String[] words=s1.split("\\s");  
    
          for(String w:words){  
             System.out.println(w);  
           }
        }
    }  

    Output-

    Java Split Method